package com.atguigu.juc.locks;

import java.util.concurrent.TimeUnit;

/**
 * @author yfstart
 * @create 2022-06-30 16:50
 */
public class DeadLockDemo {
  public static void main(String[] args) {
    final Object objectA = new Object();
    final Object objectB = new Object();

    new Thread(()->{
        synchronized (objectA){
            System.out.println(Thread.currentThread().getName()+"\t 自己持有A锁，希望获得B锁");
            try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();}
            synchronized (objectB){
                System.out.println(Thread.currentThread().getName() + "\t 成功获取到B锁");
            }
        }
    },"A").start();

    new Thread(()->{
      synchronized (objectB){
          System.out.println(Thread.currentThread().getName()+"\t 自己持有B锁，希望获得A锁");
          try { TimeUnit.MILLISECONDS.sleep(1000); } catch (InterruptedException e) { e.printStackTrace();}
          synchronized (objectA){
              System.out.println(Thread.currentThread().getName() + "\t 成功获取到A锁");
          }
      }
    },"B").start();
  }
}
